﻿using mozhi.smarterp.AuthorCenter.ApiMetadatas.Repositories;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Guids;

namespace mozhi.smarterp.AuthorCenter.ApiMetadatas
{
    public class ApiMetadataAppService : AuthorCenterAppService, IApiMetadataAppService
    {
        private IApiMetadataRepository _repository;

        private IGuidGenerator _guidGenerator;

        public ApiMetadataAppService(IApiMetadataRepository repository, IGuidGenerator guidGenerator)
        {
            _repository = repository;
            _guidGenerator = guidGenerator;
        }


        public async Task<ApiMetadataDto> GetAsync(Guid id)
        {
            var apiMetadata = await _repository.GetAsync(id);

            return ObjectMapper.Map<ApiMetadata, ApiMetadataDto>(apiMetadata);
        }

        public async Task<ApiMetadataDto> FindAsync(string scope, string path, string method)
        {
            var apiMetadata = await _repository.FindAsync(scope, path, method);

            return ObjectMapper.Map<ApiMetadata, ApiMetadataDto>(apiMetadata);
        }

        public async Task<List<ApiMetadataDto>> GetListAsync(ApiMetadataPagedResultRequestDto filter)
        {
            var apiMetadatas = await _repository.GetListAsync(filter.ApiScope, filter.Path);

            return ObjectMapper.Map<List<ApiMetadata>, List<ApiMetadataDto>>(apiMetadatas);
        }

        public async Task<PagedResultDto<ApiMetadataDto>> GetListAsync(PagedResultRequestDto filter)
        {
            var items = await _repository.GetPagedListAsync(filter.SkipCount, filter.MaxResultCount, null);

            var count = await _repository.GetCountAsync();

            var result = new PagedResultDto<ApiMetadataDto>();

            result.TotalCount = count;
            result.Items = ObjectMapper.Map<List<ApiMetadata>, List<ApiMetadataDto>>(items);

            return result;
        }

        public async Task<ApiMetadataDto> CreateAsync(ApiMetadataCreateDto input)
        {
            if (input == null)
                return null;

            var match = await _repository.FindAsync(input.Path, input.Method, input.ApiScope);

            if (match != null)
                throw new BusinessException("100000", $"接口:{input.ApiScope}_{input.Path}_{input.Method}已存在");

            match = new ApiMetadata(_guidGenerator.Create(), input.Path, input.Method, input.ApiScope);

            await _repository.InsertAsync(match);

            return ObjectMapper.Map<ApiMetadata, ApiMetadataDto>(match);
        }


        public async Task<ApiMetadataDto> UpdateAsync(ApiMetadataUpdateDto input)
        {
            if (input == null)
                return null;

            var match = await _repository.GetAsync(input.Id);

            if (match == null)
                throw new BusinessException("100000", $"接口:{input.ApiScope}_{input.Path}_{input.Method}不存在存在");

            match.Change(input.Path, input.Method, input.ApiScope);

            await _repository.UpdateAsync(match);


            return ObjectMapper.Map<ApiMetadata, ApiMetadataDto>(match);
        }

        public async Task DeleteAsync(Guid id)
        {
            await _repository.DeleteAsync(id);
        }

       
    }
}
